﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	public partial class MessageBoard : ICreator { }




	/// <summary>
	/// MessageBoards 类。
	/// </summary>
	public static partial class MessageBoards
	{
		public const string INFO_TYPE = "MessageBoard";



		#region GetItem
		/// <summary>
		/// 根据标识获取指定的留言。
		/// </summary>
		/// <param name="o">留言集合。</param>
		/// <param name="messageId">获取的留言标识 MessageId。</param>
		/// <returns>获取的信息项。</returns>
		public static MessageBoard GetItem(this System.Data.Linq.Table<MessageBoard> o, int messageId)
		{
			return o.SingleOrDefault(p => p.MessageId == messageId);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的留言是否已经存在。
		/// </summary>
		/// <param name="o">留言集合。</param>
		/// <param name="messageId">验证的留言标识 MessageId。</param>
		/// <returns>布尔值表示指定的留言是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<MessageBoard> o, int messageId)
		{
			return o.Any(p => p.MessageId == messageId);
		}
		#endregion
	}



	/// <summary>
	/// MessageBoards 类操作部分。
	/// </summary>
	public static partial class MessageBoards
	{
		#region Post
		/// <summary>
		/// 发表留言。
		/// </summary>
		/// <param name="postedMessageBoard">发表的留言。</param>
		/// <returns>发表的留言。</returns>
		public static MessageBoard Post(MessageBoard postedMessageBoard)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			MessageBoard message = postedMessageBoard.ToNewEntity<MessageBoard>();
			message.CreatorId = Users.CurrentUserId;
			message.RepContent = null;
			message.RepDate = null;
			message.IsOpen = false;
			message.IsEffective = false;
			message.IsRead = false;

			GetPostRight(dc, message, true);	// 权限验证

			dc.MessageBoards.InsertOnSubmit(message);
			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Post, INFO_TYPE, message.MessageId.ToString(), PermissionCode.Post.ToString(), message.Title);

			// 返回只读数据
			return postedMessageBoard.FillByEntity(message);
		}
		#endregion



		#region Reply
		/// <summary>
		/// 回复留言。
		/// </summary>
		/// <param name="messageId">回复的标识。</param>
		/// <param name="repContent">回复的内容。</param>
		/// <returns>回复的留言。</returns>
		public static MessageBoard Reply(int messageId, string repContent)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			MessageBoard message = dc.MessageBoards.GetItem(messageId);
			if (message == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetReplyRight(dc, message, true);	// 权限验证

			message.RepContent = repContent;
			message.RepDate = DateTime.Now;

			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Reply, INFO_TYPE, message.MessageId.ToString(), PermissionCode.Reply.ToString(), message.Title);

			// 返回只读数据
			return message.ToNewEntity<MessageBoard>();
		}
		#endregion



		#region SetOpenState
		/// <summary>
		/// 设置信息的开启状态。
		/// </summary>
		/// <param name="messageId">设置的信息标识。</param>
		/// <param name="state">设置的状态。</param>
		public static void SetOpenState(int messageId, bool state)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			MessageBoard message = dc.MessageBoards.GetItem(messageId);
			if (message == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetSetOpenStateRight(dc, message, true);	// 权限验证

			if (message.IsOpen == state)
				return;

			message.IsOpen = state;
			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Reply, INFO_TYPE, message.MessageId.ToString(), PermissionCode.Reply.ToString() + "/SetOpenState", message.Title);
		}
		#endregion



		#region Delete
		/// <summary>
		/// 删除留言。
		/// </summary>
		/// <param name="messageId">删除的留言标识 MessageId。</param>
		public static void Delete(int messageId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			MessageBoard message = dc.MessageBoards.GetItem(messageId);
			if (message == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetDeleteRight(dc, message, true);	// 权限验证

			Delete(dc, message);

			dc.SubmitChanges();

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Delete, INFO_TYPE, message.MessageId.ToString(), PermissionCode.Delete.ToString(), message.Title);
		}

		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, MessageBoard message)
		{
			dc.MessageBoards.DeleteOnSubmit(message);
		}
		#endregion
		#endregion



		#region SetEffectiveState
		/// <summary>
		/// 设置信息的有效状态。
		/// </summary>
		/// <param name="messageId">设置的信息标识。</param>
		/// <param name="state">设置的状态。</param>
		public static void SetEffectiveState(int messageId, bool state)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			MessageBoard message = dc.MessageBoards.GetItem(messageId);
			if (message == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);

			GetSetEffectiveStateRight(dc, message, true);	// 权限验证

			if (message.IsEffective == state)
				return;

			message.IsEffective = state;
			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}

			// 记录日志
			Logs.Insert(dc, ResourceId, (int)PermissionCode.Setting, INFO_TYPE, message.MessageId.ToString(), PermissionCode.Setting.ToString() + "/EffectiveState", message.Title);
		}
		#endregion
	}



	/// <summary>
	/// MessageBoards 类对应操作权限部分。（受限站点资源。）
	/// </summary>
	public static partial class MessageBoards
	{
		#region GetPostRight
		/// <summary>
		/// 获取当前用户是否有发表留言的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="message">发表的留言对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有发表留言的权限。</returns>
		public static bool GetPostRight(GenericModuleDataContext dc, MessageBoard message, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Post;
			bool b = dc.AccessController.GetUserIsHasPermission<MessageBoard>(userName, message, ResourceId, permissionCode, message.SiteId);

			if (triggerException && !b)
				throw new PermissionException(userName, message, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有发表留言的权限。
		/// </summary>
		/// <param name="message">发表的留言对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有发表留言的权限。</returns>
		public static bool GetPostRight(MessageBoard message, bool triggerException)
		{
			return GetPostRight(new GenericModuleDataContext(), message, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有发表留言的权限。
		/// </summary>
		/// <param name="message">发表的留言对象。</param>
		/// <returns>布尔值表示当前用户是否有发表留言的权限。</returns>
		public static bool GetPostRight(MessageBoard message)
		{
			return GetPostRight(message, false);
		}
		#endregion



		#region GetReplyRight
		/// <summary>
		/// 获取当前用户是否有回复留言的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="message">回复的留言对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有回复留言的权限。</returns>
		public static bool GetReplyRight(GenericModuleDataContext dc, MessageBoard message, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Reply;
			bool b = dc.AccessController.GetUserIsHasPermission<MessageBoard>(userName, message, ResourceId, permissionCode, message.SiteId);

			if (triggerException && !b)
				throw new PermissionException(userName, message, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有回复留言的权限。
		/// </summary>
		/// <param name="message">回复的留言对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有回复留言的权限。</returns>
		public static bool GetReplyRight(MessageBoard message, bool triggerException)
		{
			return GetReplyRight(new GenericModuleDataContext(), message, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有回复留言的权限。
		/// </summary>
		/// <param name="message">回复的留言对象。</param>
		/// <returns>布尔值表示当前用户是否有回复留言的权限。</returns>
		public static bool GetReplyRight(MessageBoard message)
		{
			return GetReplyRight(message, false);
		}

		public static bool GetSetOpenStateRight(GenericModuleDataContext dc, MessageBoard message, bool triggerException)
		{
			return GetReplyRight(dc, message, triggerException);
		}

		public static bool GetSetOpenStateRight(MessageBoard message, bool triggerException)
		{
			return GetSetOpenStateRight(new GenericModuleDataContext(), message, triggerException);
		}

		public static bool GetSetOpenStateRight(MessageBoard message)
		{
			return GetSetOpenStateRight(message, false);
		}
		#endregion



		#region GetDeleteRight
		/// <summary>
		/// 获取当前用户是否有删除留言的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="message">删除的留言对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除留言的权限。</returns>
		public static bool GetDeleteRight(GenericModuleDataContext dc, MessageBoard message, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Delete;
			bool b = dc.AccessController.GetUserIsHasPermission<MessageBoard>(userName, message, ResourceId, permissionCode, message.SiteId);

			if (triggerException && !b)
				throw new PermissionException(userName, message, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有删除留言的权限。
		/// </summary>
		/// <param name="message">删除的留言对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有删除留言的权限。</returns>
		public static bool GetDeleteRight(MessageBoard message, bool triggerException)
		{
			return GetDeleteRight(new GenericModuleDataContext(), message, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有删除留言的权限。
		/// </summary>
		/// <param name="message">删除的留言对象。</param>
		/// <returns>布尔值表示当前用户是否有删除留言的权限。</returns>
		public static bool GetDeleteRight(MessageBoard message)
		{
			return GetDeleteRight(message, false);
		}
		#endregion



		#region GetSettingRight
		/// <summary>
		/// 获取当前用户是否有设置留言的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="message">设置的留言对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有设置留言的权限。</returns>
		public static bool GetSettingRight(GenericModuleDataContext dc, MessageBoard message, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Setting;
			bool b = dc.AccessController.GetUserIsHasPermission<MessageBoard>(userName, message, ResourceId, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, message, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有设置留言的权限。
		/// </summary>
		/// <param name="message">设置的留言对象。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有设置留言的权限。</returns>
		public static bool GetSettingRight(MessageBoard message, bool triggerException)
		{
			return GetSettingRight(new GenericModuleDataContext(), message, triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有设置留言的权限。
		/// </summary>
		/// <param name="message">设置的留言对象。</param>
		/// <returns>布尔值表示当前用户是否有设置留言的权限。</returns>
		public static bool GetSettingRight(MessageBoard message)
		{
			return GetSettingRight(message, false);
		}



		public static bool GetSetEffectiveStateRight(GenericModuleDataContext dc, MessageBoard message, bool triggerException)
		{
			return GetSettingRight(dc, message, triggerException);
		}

		public static bool GetSetEffectiveStateRight(MessageBoard message, bool triggerException)
		{
			return GetSetEffectiveStateRight(new GenericModuleDataContext(), message, triggerException);
		}

		public static bool GetSetEffectiveStateRight(MessageBoard message)
		{
			return GetSetEffectiveStateRight(message, false);
		}
		#endregion
	}




	/// <summary>
	/// MessageBoards 类后台管理部分。
	/// </summary>
	public static partial class MessageBoards
	{
		public static bool GetManageListRight()
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			return dc.AccessController.GetUserIsHasPermission<object>(Users.CurrentUserName, null, MessageBoards.ResourceId, (int)MessageBoards.PermissionCode.Setting, App.Context.CurrentSiteId);
		}

		public static bool GetManageListRight(int? siteId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			return dc.AccessController.GetUserIsHasPermission<object>(Users.CurrentUserName, null, MessageBoards.ResourceId, (int)MessageBoards.PermissionCode.Setting, siteId);
		}
	}
}